<?php


namespace Tbf\Bundle\CallbackSerializerBundle\Wrapper;


use Symfony\Component\DependencyInjection\ContainerInterface;
use Tbf\Bundle\CallbackSerializerBundle\CallbackInterface;

class DicServiceWrapper implements CallbackInterface{
    protected $service_id;
    protected $function_name;
    /**
     * @var ContainerInterface
     */
    protected $container;
    function __construct($service_id,$function_name){
        $this->service_id = $service_id;
        $this->function_name = $function_name;
    }

    /**
     * @param ContainerInterface $container
     */
    function setContainer(ContainerInterface $container){
        $this->container = $container;
    }
    function invoke($input=null){
        if (!$this->container->has($this->service_id)){
            throw new \Exception(
                sprintf("DicServiceWrapper: service not found! service_id:%s\n",
                    $this->service_id,
                    $this->function_name
                ));
        }
        $service = $this->container->get($this->service_id);
        if (!method_exists($service,$this->function_name)){
            throw new \Exception(
                sprintf("DicServiceWrapper: function not found! \nservice_id: %s\nfunciton_name: %s",
                    $this->service_id,
                    $this->function_name
                ));
        }
        return call_user_func(
            array($service,$this->function_name),
            $input);
    }

    /**
     * @return mixed
     */
    public function getFunctionName()
    {
        return $this->function_name;
    }

    /**
     * @return mixed
     */
    public function getServiceId()
    {
        return $this->service_id;
    }

}